<template>
  <div class="app-container">
    <div style="display: flex; width: 120%">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform5" :formList="formList" @findClicked="findClicked" />
      <Find
        :OriginalFilter="{ static: true }"
        style="margin-top: 19px; margin-left: 15px"
        :searchImg="false"
        @Launchform="Launchform"
        :formList="formList1"
        @findClicked="findClicked1"
      />
    </div>

    <div style="margin-top: 15px">
      <el-card shadow="never" :body-style="{ padding: '13px' }">
        <div slot="header">
          <span style="font-weight: 600; color: red">输入待订购明细</span>
          <el-button style="margin-left: 15px" type="primary" size="default" @click="handleCloseMateriaLKlc as any">删除</el-button>
        </div>

        <Table
          @selectListShow="selectListShow"
          @selectValChange="selectedList"
          @inputChange="checkboxChange2"
          @selectList="selectList"
          :isLoading="isLoading1"
          :tableHeader="tableHeader"
          :tableData="ToBeInstructedTableDate"
          selection
          :TableHeight="32"
        />

        <PageModule :PageData="{ buttonS: true, total: totalList }" @pageChange="pageChange1" @pageSizeChange="pageSizeChange1" />
      </el-card>
    </div>

    <Find
      :OriginalFilter="{ static: true }"
      @clickNumber1="radio1"
      :searchImg="false"
      @Launchform="Launchform1"
      :formList="formList2"
      @findClicked="findClicked2"
    />
    <div style="margin-top: 10px">
      <el-card shadow="never" :body-style="{ padding: '13px' }">
        <div slot="header" class="f16 mb18">
          <span style="font-weight: 600; color: blue">已订购明细</span>
        </div>
        <Table
          @selectValChange="selectedList1"
          @currentRow="clicktogetdata"
          @inputChange="confirmChange10"
          @selectList="selectList1"
          :isLoading="isLoading"
          :tableHeader="tableHeader2"
          :tableData="OrderedDetailsTableDate"
          :TableHeight="32"
        />
        <PageModule :PageData="{ buttonS: true, total }" @pageChange="pageChange" @pageSizeChange="pageSizeChange" />
      </el-card>
    </div>

    <!-- 批量改序 -->
    <el-dialog :close-on-click-modal="false" v-model="dialogVisible8" width="50%">
      <div style="display: flex">
        <div style="width: 30%">
          <span style="margin-right: 10px; margin-top: 15px; height: 25px">已订购明细</span>
          <el-input
            v-model="prisce.PurDate"
            :disabled="true"
            placeholder=""
            size="small"
            clearable
            @change=""
            style="width: 120px; height: 30px; margin-top: 10px"
          />
          <p>
            <span style="margin-right: 10px; margin-top: 10px; height: 25px">原单价</span>
            <el-input
              v-model="prisce.Price"
              :disabled="true"
              placeholder=""
              size="small"
              clearable
              @change=""
              style="width: 120px; height: 30px; margin-top: 10px; margin-left: 30px"
            />
          </p>
        </div>

        <div style="width: 30%">
          <span style="margin-right: 10px; margin-top: 15px; height: 25px">新订购日期</span>
          <el-date-picker
            v-model="newprisce.newPurDate"
            type="date"
            size="small"
            clearable
            style="width: 120px; height: 30px; margin-top: 10px"
            :placeholder="formatCurrentDate(currentDate)"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change=""
          ></el-date-picker>
          <p>
            <span style="margin-right: 10px; margin-top: 10px; height: 25px">新单价</span>
            <el-input
              v-model="newprisce.newPrice"
              placeholder=""
              size="small"
              clearable
              @change=""
              style="width: 120px; height: 30px; margin-top: 10px; margin-left: 30px"
            />
          </p>
        </div>
      </div>
      <el-button style="margin-top: 15px; margin-left: 15px" type="primary" size="default" @click="ubDateList">保存</el-button>
    </el-dialog>

    <!-- 新增待订购明细 -->
    <el-dialog :close-on-click-modal="false" v-model="dialogVisible7" width="50%">
      <Find
        :OriginalFilter="{ static: true }"
        @clickNumber1="radio1"
        :searchImg="false"
        @Launchform="Launchform3"
        :formList="formList3"
        @findClicked="findClicked3"
      />
      <Table
        @selectList="selectList2"
        @currentRow1="currentRowList1"
        :isLoading="isLoading"
        :tableHeader="tableHeader3"
        :tableData="NewOrderDetailsTableDate"
        selection
        :TableHeight="34"
      />
      <PageModule :PageData="{ buttonS: true, total: totalList1 }" @pageChange="pageChange11" @pageSizeChange="pageSizeChange11" />
      <h1 style="margin-top: 15px; color: black; font-size: 16px">物料资料建档</h1>
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList5" @Launchform="Launchform7" @findClicked="findData5" />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { styleQuery } from '@/api/Control/FactoryControl/FormingPre'
import { ManufacturerQuery } from '@/api/PurchasingDepartment/FactoryPurchasing/MaterialRequisition'
import { packingMaterials } from '@/api/PurchasingDepartment/FactoryPurchasing/PackagingMaterialCategoryDefinition'
import {
  deleteList,
  styleQyert,
  lotNoListQuery,
  dbClickAdd,
  ToBeInstructed,
  CheckTheTapeOrderJob,
  OrderedDetails,
  PurchaseOrderCancellation,
  PurchaseOrderCancellationList,
  NewOrderDetails,
  MaterialClassificationQuery,
  GeneratedMaterialCoding,
  AddGeneratedMaterialCoding
} from '@/api/PurchasingDepartment/FactoryPurchasing/PackagingMaterialsOtherOrder'
import { GainVendor } from '@/api/WarehouseDepartment/FactoryStorage/ConsumablesIn'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineComponent, onMounted, ref } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PackagingMaterialsOtherOrder'
})
</script>
<script lang="ts" setup>
const isLoading1 = ref(false)

// 删除待定购明细
const deleteListParms = ref<any>([])

const handleCloseMateriaLKlc = (_done?: () => void) => {
  if (deleteListParms.value.length === 0) {
    JMessage.warning('请勾选需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deleteListMethods()
      })
      .catch(() => {})
  }
}

const deleteListMethods = async () => {
  const result = await deleteList(deleteListParms.value)
  if (result.code === 200) {
    ToBeInstructedMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 指令
const totalListpartName = ref(0)

const listanyTableDateSelect = ref([])

const listanyTableDateSelect1 = ref([])

const avtive = ref(false)

const selectListShow = (row?: any, visible?: boolean) => {
  if (visible) {
    row.lotNo = {
      selectValue: row.lotNo.selectValue,
      options: listanyTableDateSelect1,
      PageModule: true,
      total: totalListpartName,
      handleSizeChange: (val: any) => {
        lotNoListQueryParms1.value.pageNum = val
        lotNoListQueryMethods1()
      },
      handleCurrentChange: (val: any) => {
        lotNoListQueryParms1.value.pageNum = val
        lotNoListQueryMethods1()
      }
    }
  } else {
    avtive.value = true
    if (row !== undefined && row.lotNo === Object) {
      row.lotNo.options = []
    }
  }
}

const styleQyertParms = ref('')

const styleQyertMethods = async (_: any) => {
  const result = await styleQyert(styleQyertParms.value)
  if (result.length !== 0) {
    _.style = result[0].style
  }

  checkboxChange2(_)
}

// 指令回显
const lotNoListQueryParms = ref({
  lotNo: '',
  pageSize: 40,
  pageNum: 1
})

const lotNoListQueryParms1 = ref({
  lotNo: '',
  pageSize: 40,
  pageNum: 1
})

const lotNoListQueryMethods1 = async () => {
  const result = await lotNoListQuery(lotNoListQueryParms1.value)
  const NewArrayList = result.lotNoList.reduce((arr: any, item: any) => {
    if (item.lotNo) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.lotNo)
    }
    return arr
  }, [])
  totalListpartName.value = result.total
  listanyTableDateSelect1.value = EvaluatesTheArrayReturnValue1(NewArrayList)
}

const selectedList = (_: any, row: any) => {
  styleQyertParms.value = _.lotNo.selectValue
  styleQyertMethods(_)
}

const selectedList1 = (_: any, row: any) => {
  confirmChange10(_)
}

const totalLo = ref(0)

const lotNoListQueryMethods = async () => {
  const result = await lotNoListQuery(lotNoListQueryParms.value)
  const NewArrayList = result.lotNoList.reduce((arr: any, item: any) => {
    if (item.lotNo) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.lotNo)
    }
    return arr
  }, [])
  totalLo.value = result.total
  formList.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
  listanyTableDateSelect.value = EvaluatesTheArrayReturnValue1(NewArrayList)
}

const handleSizeChange1 = (val: number) => {
  lotNoListQueryParms.value.pageSize = val
  lotNoListQueryMethods()
}
const handleCurrentChange1 = (val: number) => {
  lotNoListQueryParms.value.pageNum = val
  lotNoListQueryMethods()
}

const handleSizeChange = (val: number) => {
  lotNoListQueryParms1.value.pageSize = val
  lotNoListQueryMethods1()
}
const handleCurrentChange = (val: number) => {
  lotNoListQueryParms1.value.pageNum = val
  lotNoListQueryMethods1()
}

// 待订购明细编辑
const checkboxChange2 = async (list: any) => {
  list.purType = list.purType.selectValue
  list.lotNo = list.lotNo.selectValue
  const result = await LoginWork.post<any, any>(`/PackagOtherOrderingOperation/updateBatchNotOrderMaterial`, [list])
  if (result.code === 200) {
    ToBeInstructedMethods()
    JMessage.success(result.message)
  } else {
    ToBeInstructedMethods()
    JMessage.error(result.message)
  }
}

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

const styleQueryMethods = async () => {
  let result = await styleQuery()

  formList.value[1].selectOptions = EvaluatesTheArrayReturnValue(result.filter((item: any) => item !== null))
  listanyTableDateSelect1.value = EvaluatesTheArrayReturnValue1(result.filter((item: any) => item !== null))
}

// 双击料号新增
const dbClickAddMethodsParms = ref([])

const selectList2 = (row: any) => {
  dbClickAddMethodsParms.value = row.map((item: any) => {
    return item
  })
}

const findClicked3 = (count: string) => {
  if (count === '选择材料') {
    if (dbClickAddMethodsParms.value.length === 0) {
      JMessage.warning('请勾选需要添加的数据')
    } else {
      dbClickAddMethods()
    }
  }
}

const dbClickAddMethods = async () => {
  const result = await dbClickAdd(dbClickAddMethodsParms.value)
  if (result.code === 200) {
    ToBeInstructedMethods()
    dialogVisible7.value = false
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 物料资料建档
const formList5 = ref([
  {
    type: 'input',
    inputPlaceholder: '物料编号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '品名',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '单位',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '规格',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '颜色',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '毛重',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '净重',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '清空'
  },
  {
    type: 'button',
    checkboxLabel: '生成物料编码'
  },
  {
    type: 'button',
    checkboxLabel: '增加'
  }
])

// 增加
const AddGeneratedMaterialCodingParms = ref({
  grossWeight: '',
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlType: '',
  mtlTypeName: '',
  mtlUnit: '',
  netWeight: ''
})

const Launchform7 = (row: any) => {
  AddGeneratedMaterialCodingParms.value.mtlNo = row[0].inputValue
  AddGeneratedMaterialCodingParms.value.mtlName = row[1].inputValue
  AddGeneratedMaterialCodingParms.value.mtlUnit = row[2].inputValue
  AddGeneratedMaterialCodingParms.value.mtlType = row[3].selectValue.split(' ')[0]
  AddGeneratedMaterialCodingParms.value.mtlTypeName = row[3].selectValue.split(' ')[1]
  AddGeneratedMaterialCodingParms.value.mtlSpec = row[4].inputValue
  AddGeneratedMaterialCodingParms.value.mtlColor = row[5].inputValue
  AddGeneratedMaterialCodingParms.value.grossWeight = row[6].inputValue
  AddGeneratedMaterialCodingParms.value.netWeight = row[7].inputValue

  GeneratedMaterialCodingParms.value.mtlNo = row[0].inputValue
  GeneratedMaterialCodingParms.value.mtlName = row[1].inputValue
  GeneratedMaterialCodingParms.value.mtlUnit = row[2].inputValue
  GeneratedMaterialCodingParms.value.mtlType = row[3].selectValue.split(' ')[0]
  GeneratedMaterialCodingParms.value.mtlTypeName = row[3].selectValue.split(' ')[1]
  GeneratedMaterialCodingParms.value.mtlSpec = row[4].inputValue
  GeneratedMaterialCodingParms.value.mtlColor = row[5].inputValue
  GeneratedMaterialCodingParms.value.grossWeight = row[6].inputValue
  GeneratedMaterialCodingParms.value.netWeight = row[7].inputValue
}

const AddGeneratedMaterialCodingMethods = async () => {
  const result = await AddGeneratedMaterialCoding(AddGeneratedMaterialCodingParms.value)
  if (result.code === 200) {
    clearFormList1()
    NewOrderDetailsMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 生成物料编码
const GeneratedMaterialCodingParms = ref({
  grossWeight: '',
  mtlColor: '',
  mtlName: '',
  mtlSpec: '',
  mtlType: '',
  mtlTypeName: '',
  mtlUnit: '',
  netWeight: '',
  mtlNo: ''
})

const GeneratedMaterialCodingMethods = async () => {
  const result = await GeneratedMaterialCoding(GeneratedMaterialCodingParms.value)
  if (result.code === 200) {
    formList5.value[0].inputValue = result.data.mtlNo
    JMessage.success(result.message)
  } else {
    NewOrderDetailsMethods()
    JMessage.error(result.message)
  }
}

const currentRowList1 = (row: any) => {
  formList5.value[1].inputValue = row.mtlName
  formList5.value[2].inputValue = row.mtlUnit
  formList5.value[3].selectValue = row.mtlType
  formList5.value[4].inputValue = row.mtlSpec
  formList5.value[5].inputValue = row.mtlColor
  formList5.value[6].inputValue = row.grossWeight
  formList5.value[7].inputValue = row.netWeight
}

const clearFormList1 = () => {
  formList5.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const findData5 = (count: string) => {
  if (count === '生成物料编码') {
    GeneratedMaterialCodingMethods()
  }
  if (count === '清空') {
    clearFormList1()
  }
  if (count === '增加') {
    if (AddGeneratedMaterialCodingParms.value.mtlName === '' || AddGeneratedMaterialCodingParms.value.mtlNo === '') {
      JMessage.warning('物料编号物料名称颜色不可为空')
    } else {
      AddGeneratedMaterialCodingMethods()
    }
  }
}

// 材料分类查询
const MaterialClassificationQueryMethods = async () => {
  const result = await MaterialClassificationQuery()
  const NewArrayList = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])

  formList3.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
  formList5.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
}

// 新增订购明细查询
const NewOrderDetailsParms = ref({
  grossWeight: '',
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlType: '',
  mtlTypeName: '',
  mtlUnit: '',
  netWeight: '',
  pageNum: 1,
  pageSize: 40
})

const Launchform3 = (row: any) => {
  NewOrderDetailsParms.value.mtlType = row[0].selectValue.split(' ')[0]
  NewOrderDetailsParms.value.mtlTypeName = row[0].selectValue.split(' ')[1]
  NewOrderDetailsParms.value.mtlName = row[1].inputValue
  NewOrderDetailsParms.value.mtlSpec = row[2].inputValue
  NewOrderDetailsMethods()
}

const isLoading2 = ref(false)

const NewOrderDetailsTableDate = ref([])

const NewOrderDetailsMethods = async () => {
  isLoading2.value = true
  const result = await NewOrderDetails(NewOrderDetailsParms.value)
  NewOrderDetailsTableDate.value = result.lotNoList
  totalList1.value = result.total
  isLoading2.value = false
}

const totalList1 = ref(0)

const pageChange11 = (val: any) => {
  NewOrderDetailsParms.value.pageNum = val
  NewOrderDetailsMethods()
}

const pageSizeChange11 = (val: any) => {
  NewOrderDetailsParms.value.pageSize = val
  NewOrderDetailsMethods()
}

const dialogVisible7 = ref(false)

// 新增
const formList3 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '材料分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '规格',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '选择材料'
  }
])

// 材料类型
// 部门 const result = await FindDate()
const ListArray = ref<any>([
  { optionKey: 1, optionValue: '通用', optionLabel: '通用' },
  { optionKey: 2, optionValue: '补单', optionLabel: '补单' },
  { optionKey: 3, optionValue: '样品单', optionLabel: '样品单' }
])

// 日期
const formatCurrentDate = (date?: any) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}-${month}-${day}`
}

const currentDate = ref(new Date())

const newprisce = ref({
  newPrice: '',
  newPurDate: ''
})

const prisce = ref({
  Price: '',
  PurDate: ''
})

const ubDateList = async (row?: any) => {
  if (newprisce.value.newPrice === '' || newprisce.value.newPurDate === '') {
    JMessage.warning('数据不完整')
  } else {
    const result = await LoginWork.post<any, any>(
      `/PackagOtherOrderingOperation/updateReqDateAndUPrice`,
      Object.assign(
        {},
        {
          newPrice: newprisce.value.newPrice,
          newPurDate: newprisce.value.newPurDate,
          request: updateTime.value
        }
      )
    )
    if (result.code === 200) {
      dialogVisible8.value = false
      newprisce.value.newPrice = ''
      newprisce.value.newPurDate = ''
      OrderedDetailsMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

const clicktogetdata = (row: any) => {
  prisce.value.Price = row.price
  prisce.value.PurDate = row.reqDate
  updateTime.value = row
  PurchaseOrderCancellationParms.value = row.purNo
  purNoList.value.purNo = row.purNo
  purNoList.value.vendorName = row.vendorName
}
const purNoList = ref({
  purNo: '',
  vendorName: ''
})

const updateTime = ref({})

// 修改日期/单价
const dialogVisible8 = ref(false)

// 带订购明细修改
const confirmChange10 = async (row?: any) => {
  row.purType = row.purType.selectValue
  const result = await LoginWork.post<any, any>(`/PackagOtherOrderingOperation/updateOrderedRemark`, [row])
  if (result.code === 200) {
    OrderedDetailsMethods()
    JMessage.success(result.message)
  } else {
    OrderedDetailsMethods()
    JMessage.error(result.message)
  }
}

// 生成订购单
const PurchaseOrderCancellationListParms = ref<any>([])

const PurchaseOrderCancellationListMethods = async () => {
  PurchaseOrderCancellationListParms.value = PurchaseOrderCancellationListParms.value.map((item: any) => {
    item.purType = item.purType.selectValue
    item.lotNo = item.lotNo.selectValue
    return item
  })
  const result = await PurchaseOrderCancellationList(PurchaseOrderCancellationListParms.value)
  if (result.code === 200) {
    OrderedDetailsMethods()
    PurchaseOrderCancellationListParms.value = []
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 订购单取消
const PurchaseOrderCancellationParms = ref<any>('')

const selectList1 = (row: any) => {}

const findClicked2 = async (count: string) => {
  if (count === '生成订购单') {
    if (PurchaseOrderCancellationListParms.value.length === 0) {
      JMessage.warning('请选择需要生成的订购单')
    } else {
      PurchaseOrderCancellationListMethods()
    }
  }
  if (count === '置空') {
    claerInputValueAndSelectValue1()
  }
  if (count === '修改需求日期/单价') {
    if (prisce.value.PurDate === '') {
      JMessage.warning('请选择需要修改的数据')
    } else {
      dialogVisible8.value = true
    }
  }

  if (count === '导出订购单(分段/纵)') {
    if (purNoList.value.purNo === '') {
      JMessage.warning('订购单号不可为空')
    } else {
      const res = await fileRequest.post<any, any>(`/PackagOtherOrderingOperation/exportPurchaseOrderByLongitudinal`, purNoList.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
  if (count === '导出订购单') {
    if (purNoList.value.purNo === '') {
      JMessage.warning('订购单号不可为空')
    } else {
      const res = await fileRequest.post<any, any>(`/PackagOtherOrderingOperation/exportPurchaseOrder`, purNoList.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
  if (count === '导出订购单(分段/横)') {
    if (purNoList.value.purNo === '') {
      JMessage.warning('订购单号不可为空')
    } else {
      let hasNullMtlSpec = OrderedDetailsTableDate.value.every((item: any) => item.mtlSpec === null)

      if (hasNullMtlSpec) {
        JMessage.warning('规格为空的数据不可导出分段/横')
      } else {
        const res = await fileRequest.post<any, any>(`/PackagOtherOrderingOperation/exportPurchaseOrderByTransverse`, purNoList.value)
        const link = document.createElement('a') // 利用a标签自定义下载文件名
        let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
        link.style.display = 'none'
        //设置连接
        link.href = window.URL.createObjectURL(blob)
        const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
        link.download = FileName //自定义文件名字
        document.body.appendChild(link)
        //模拟点击事件
        link.click()
        document.body.removeChild(link)
      }
    }
  }
}

const claerInputValueAndSelectValue1 = () => {
  formList2.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
  })
}

const PurchaseOrderCancellationMethods = async () => {
  const result = await PurchaseOrderCancellation(PurchaseOrderCancellationParms.value)
  if (result.code === 200) {
    ToBeInstructedMethods()
    OrderedDetailsMethods()
    PurchaseOrderCancellationParms.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const radio1 = (row: string) => {
  switch (row) {
    case '0': {
      OrderedDetailsParms.value.flag = 2
      break
    }
    case '1': {
      OrderedDetailsParms.value.flag = 0
      break
    }
    case '2': {
      OrderedDetailsParms.value.flag = 1
      break
    }
  }
  OrderedDetailsMethods()
}

const inputValueListN = ref(0)

const isLoading = ref(false)

// 已订购明细查询
const OrderedDetailsParms = ref({
  flag: 2,
  lotNo: '',
  mtlName: '',
  mtlSpec: '',
  pageNum: 0,
  pageSize: 40,
  purDateEnd: '',
  purDateStart: '',
  purNo: '',
  reqDateEnd: '',
  reqDateStart: '',
  shipmentName: '',
  style: '',
  typeName: '',
  typeNo: '',
  vendorName: '',
  purType: ''
})

const Launchform1 = (row: any) => {
  OrderedDetailsParms.value.purType = row[0].inputValue
  OrderedDetailsParms.value.lotNo = row[1].inputValue
  OrderedDetailsParms.value.style = row[2].inputValue
  OrderedDetailsParms.value.purNo = row[3].inputValue
  OrderedDetailsParms.value.vendorName = row[4].selectValue.split(' ')[1]

  OrderedDetailsParms.value.mtlName = row[5].inputValue
  OrderedDetailsParms.value.purDateStart = row[6].dateValue[0]
  OrderedDetailsParms.value.purDateEnd = row[6].dateValue[1]
  OrderedDetailsParms.value.reqDateStart = row[7].dateValue[0]
  OrderedDetailsParms.value.reqDateEnd = row[7].dateValue[1]
  OrderedDetailsMethods()
}

const total = ref(0)

const pageChange = (val: any) => {
  OrderedDetailsParms.value.pageNum = val
  OrderedDetailsMethods()
}

const pageSizeChange = (val: any) => {
  OrderedDetailsParms.value.pageSize = val
  OrderedDetailsMethods()
}

const OrderedDetailsTableDate = ref([])

const OrderedDetailsMethods = async () => {
  isLoading.value = true
  const result = await OrderedDetails(OrderedDetailsParms.value)
  OrderedDetailsTableDate.value = result.lotNoList.map((item: any) => {
    item.purType = {
      selectValue: item.purType || '',
      options: [
        { value: '通用', label: '通用', item: '1' },
        { value: '补单', label: '补单', item: '2' },
        { value: '样品单', label: '样品单', item: '3' },
        { value: '', label: '', item: '4' }
      ]
    }
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })

  total.value = result.total
  isLoading.value = false
}

// 勾选待订购作业
const CheckTheTapeOrderJobParms = ref<any>({
  date: '',
  request: [],
  vendorName: '',
  vendorNo: ''
}) as any

const Launchform = (row: any) => {
  CheckTheTapeOrderJobParms.value.date = row[1].dateValue
  CheckTheTapeOrderJobParms.value.vendorNo = row[0].selectValue.split(' ')[0]
  CheckTheTapeOrderJobParms.value.vendorName = row[0].selectValue.split(' ')[1]
}

const findClicked1 = (count: string) => {
  if (count === '生成订购单') {
    if (CheckTheTapeOrderJobParms.value.request.length === 0) {
      JMessage.warning('请勾选需要待订购的数据')
    } else if (CheckTheTapeOrderJobParms.value.vendorNo === '') {
      JMessage.warning('请选择正确的厂商')
    } else if (CheckTheTapeOrderJobParms.value.date === '') {
      JMessage.warning('请选择正确的日期')
    } else {
      CheckTheTapeOrderJobMethods()
    }
  }
  if (count === '新增输入待订购明细') {
    dialogVisible7.value = true
    NewOrderDetailsMethods()
    MaterialClassificationQueryMethods()
  }
  if (count === '订购单取消') {
    if (PurchaseOrderCancellationParms.value === '') {
      JMessage.warning('请选择需要取消的订购单')
    } else {
      PurchaseOrderCancellationMethods()
    }
  }
}

const CheckTheTapeOrderJobMethods = async () => {
  CheckTheTapeOrderJobParms.value.request = CheckTheTapeOrderJobParms.value.request.map((item: any) => {
    item.purType = item.purType.selectValue
    item.lotNo = item.lotNo.selectValue
    return item
  })

  const result = await CheckTheTapeOrderJob(CheckTheTapeOrderJobParms.value)
  if (result.code === 200) {
    if (CheckTheTapeOrderJobParms.vlaue !== undefined) {
      CheckTheTapeOrderJobParms.vlaue.request = []
    }

    ToBeInstructedMethods()
    OrderedDetailsMethods()
    JMessage.success(result.message)
  } else {
    ToBeInstructedMethods()
    OrderedDetailsMethods()
    JMessage.error(result.message)
  }
}

const selectList = (selectedRows: any[]) => {
  deleteListParms.value = selectedRows.map((item: any) => {
    return item.id
  })

  CheckTheTapeOrderJobParms.value.request = selectedRows.map((item: any) => {
    return item
  })

  let totalQty = 0

  selectedRows.forEach((item: any) => {
    totalQty += item.purQty
  })

  inputValueListNum.value = totalQty

  PurchaseOrderCancellationParms.value = selectedRows.map((item: any) => {
    return item
  })
}

const inputValueListNum = ref(0)

// 查询
const findClicked = (count: string) => {
  if (count === '置空') {
    claerInputValueAndSelectValue()
  }
}

const claerInputValueAndSelectValue = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const Launchform5 = (row: any) => {
  if (avtive) {
    ToBeInstructedParms.value.lotNo = row[0].selectValue
    ToBeInstructedParms.value.style = row[1].selectValue
    ToBeInstructedParms.value.typeName = row[2].selectValue.split(' ')[1]
    ToBeInstructedParms.value.typeNo = row[2].selectValue.split(' ')[0]
    ToBeInstructedParms.value.mtlName = row[3].inputValue
    ToBeInstructedParms.value.mtlSpec = row[4].inputValue
    ToBeInstructedMethods()
  }
}

// 包材订购明细查询
const ToBeInstructedParms = ref({
  flag: '',
  lotNo: '',
  mtlName: '',
  mtlSpec: '',
  pageNum: 1,
  pageSize: 40,
  purDateEnd: '',
  purDateStart: '',
  purNo: '',
  reqDateEnd: '',
  reqDateStart: '',
  shipmentName: '',
  style: '',
  typeName: '',
  typeNo: '',
  vendorName: ''
})

const ToBeInstructedTableDate = ref([])

const ToBeInstructedMethods = async () => {
  isLoading1.value = true
  let total = 0
  const result = await ToBeInstructed(ToBeInstructedParms.value)
  ToBeInstructedTableDate.value = result.lotNoList.map((item: any, index: number) => {
    item.change = 'Y'
    item.disableConfirm = true
    if (item.purFlag === 'Y') {
      item.purFlag = true
    }
    item.purType = {
      selectValue: item.purType || '',
      options: [
        { value: '通用', label: '通用', item: '1' },
        { value: '补单', label: '补单', item: '2' },
        { value: '样品单', label: '样品单', item: '3' },
        { value: '', label: '', item: '4' }
      ]
    }

    item.lotNo = {
      selectValue: item.lotNo || '',
      options: []
    }

    return item
  })
  inputValueListN.value = total
  totalList.value = result.total
  isLoading1.value = false
}

const totalList = ref(0)

const pageChange1 = (val: any) => {
  ToBeInstructedParms.value.pageNum = val
  ToBeInstructedMethods()
}

const pageSizeChange1 = (val: any) => {
  ToBeInstructedParms.value.pageSize = val
  ToBeInstructedMethods()
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const packingMaterialsMethods = async () => {
  const result = await packingMaterials()

  let array = result.lotNoList.map((item: any) => {
    delete item.id
    delete item.caluBase
    delete item.sizeFlag
    delete item.purFlag

    return item
  })

  const NewArrayList = array.reduce((arr: any, item: any) => {
    if (item.seqNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.seqNo + ' ' + item.typeName)
    }
    return arr
  }, [])

  formList.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
}

const VenderNameList = async () => {
  const result = await ManufacturerQuery()
  const NewArrayList = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])

  formList1.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
  formList2.value[4].selectOptions = EvaluatesTheArrayReturnValue(NewArrayList)
}

onMounted(() => {
  VenderNameList()

  packingMaterialsMethods()

  ToBeInstructedMethods()

  OrderedDetailsMethods()

  lotNoListQueryMethods()

  styleQueryMethods()

  lotNoListQueryMethods1()
})

const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '指令',
    selectLabel: '',
    selectOptions: [],
    PageModule: true,
    total: totalLo,
    handleSizeChange: handleSizeChange1,
    handleCurrentChange: handleCurrentChange1
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '型体',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入规格',
    inputValue: '',
    inputLabel: ''
  }
])

const formList2 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '请购类型',
    selectLabel: '',
    selectOptions: ListArray.value
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '需求日期',
    dateValue: ''
  },

  {
    type: 'button',
    checkboxLabel: '导出订购单'
  },

  {
    type: 'button',
    checkboxLabel: '导出订购单(分段/纵)'
  },
  {
    type: 'button',
    checkboxLabel: '导出订购单(分段/横)'
  },
  {
    type: 'button',
    checkboxLabel: '修改需求日期/单价'
  },

  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList1 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'dateSingle',
    dateValue: '',
    dateLabel: '交期'
  },
  {
    type: 'button',
    checkboxLabel: '生成订购单'
  },
  {
    type: 'button',
    checkboxLabel: '订购单取消'
  },
  {
    type: 'button',
    checkboxLabel: '新增输入待订购明细'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const tableHeader = ref([
  {
    label: '请购类型',
    prop: 'purType',
    // change: true,
    type: 'select'
  },
  {
    label: '指令',
    prop: 'lotNo',
    change: true,
    type: 'select'
  },
  {
    label: '型体',
    prop: 'style',
    change: true
  },
  {
    label: '包材类别',
    prop: 'typeName',
    change: true
  },
  {
    label: '料号',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true,
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '订购数量',
    prop: 'purQty'
    // change: true
  },
  {
    label: '订购单价',
    prop: 'price'
  }
])

const tableHeader2 = ref([
  {
    label: '请购类型',
    prop: 'purType',
    change: true,
    type: 'select'
  },
  {
    label: '订购单号',
    prop: 'purNo',
    change: true
  },
  {
    label: '订购日期',
    prop: 'purDate',
    change: true
  },
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    label: '厂商',
    prop: 'vendorName',
    change: true
  },
  {
    label: '需求日期',
    prop: 'reqDate',
    change: true
  },
  {
    label: '包材类别',
    prop: 'typeName',
    change: true
  },
  {
    label: '料号',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true,
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '订购数量',
    prop: 'purQty',
    change: true
  },
  {
    label: '备注',
    prop: 'purRemark'
  }
])

const tableHeader3 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo',
    change: true
  },
  {
    label: '材料类型',
    prop: 'mtlType',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true,
    start: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '毛重',
    prop: 'grossWeight',
    change: true
  },
  {
    label: '净重',
    prop: 'netWeight',
    change: true
  }
])
</script>

<style lang="less" scoped>
.main {
  width: 100%;
  background-color: white;
}

.btns {
  display: flex;
  padding: 10px 35px 5px;
  justify-content: end;

  .bg {
    background-color: #e7e7e7;
    border: 1px solid #e7e7e7;
    color: #000000;
  }
}
</style>
